

#include "ConfigChecker.h"

bool IsSameConfig(const char* db1, const char* db2)
{
    // Manage = tcp://127.0.0.1:3306,root,123, Nx_Manage
    StringArray sa, sa2;

    split_path(sa, db1, ',');
    split_path(sa2, db2, ',');

    //
    if (sa.size() != 4 || sa2.size() != 4)
    {
        printf( db2);
        printf("\n");

        return true;
    }

    if (sa[0] == sa2[0] && sa[1] == sa2[1]
        && sa[2] == sa2[2]  && sa[3] == sa2[3])
    {
        printf( db2);
        printf("\n");

        // 全部相同
        return true;
    }

    return false;
}

bool CompareWithExist( vector<const char*>& list, string& str)
{
    for (size_t i=0; i < list.size();  i++)
    {
        if (IsSameConfig( list[i], str.c_str()))
        {
            return false;
        }
    }

    list.push_back(str.c_str());
    return true;
}

ConfigChecker* _CONFIG;
CAsyncProcMgr* _PROC;

ConfigChecker g_Config;
CAsyncProcMgr g_proc;

 int g_proc_id = 1;

ConfigChecker::ConfigChecker()
{
    _CONFIG = &g_Config;
    _PROC = &g_proc;
}

ConfigChecker:: ~ConfigChecker()
{

}

bool ConfigChecker::LoadConfig()
{
    string szConfigFile;

    // 获取资源路径
	W_GetAppPath(szConfigFile);
	fix_path(szConfigFile);

	// 读取服务器配置
	szConfigFile = szConfigFile + "merge_db.ini";
	CIniFile2 ini(szConfigFile.c_str());
	if (!ini.LoadFromFile())
	{
		return false;
	}

	// 读取目标数据库
    master. Manage = ini.ReadString("master", "Manage", "");
    master. Role = ini.ReadString("master", "Role", "");
    master. Family = ini.ReadString("master", "Family", "");
    master. Master = ini.ReadString("master", "Master", "");

    master.Mail = ini.ReadString("master", "Mail","");

    if (!master.IsConfigLoaded())
    {
        printf("合并后的目标库无效");
        return false;
    }

	// 读取分区
	for(int i=1; i <= 32; i++)
	{
        char sec_name[16];
        sprintf(sec_name, "slave%d", i);

        DBConnect conn;
        conn. Manage = ini.ReadString(sec_name, "Manage", "");
        conn. Role = ini.ReadString(sec_name, "Role", "");
        conn. Family = ini.ReadString(sec_name, "Family", "");
        conn. Master = ini.ReadString(sec_name, "Master", "");
        conn.Mail = ini.ReadString(sec_name, "Mail", "");

        if (!conn.IsConfigLoaded())
        {
            continue;
        }

        slaves.push_back( conn );
	}

	if (slaves.size() < 1)
	{
	    printf("至少需要1个有效的分区才能进行合并\n");
	    return false;
	}

	// 检测是否存在重复的字符串
    if ( !CheckDBConnStr() )
    {
        printf("数据库链接地址不能重复! 请检查配置文件! merge_db.ini\n");
        return false;
    }

    if ( !CheckDBConnection() )
    {
        printf("数据库无法链接，程序即将退出！\n");
        return false;
    }

    return true;
}

 bool ConfigChecker::CheckDBConnStr()
 {
     vector<const char*> connList;

    if (!CompareWithExist( connList,  master. Manage)) return false;
    if (!CompareWithExist(connList,  master. Role)) return false;
    if (!CompareWithExist(connList,  master. Family)) return false;
    if (!CompareWithExist(connList,  master. Master)) return false;
    if (!CompareWithExist(connList,  master. Mail)) return false;

    for (size_t i=0; i < slaves.size(); i++)
    {
        if (!CompareWithExist( connList,  slaves[i]. Manage)) return false;
        if (!CompareWithExist(connList,  slaves[i]. Role)) return false;
        if (!CompareWithExist(connList,  slaves[i]. Family)) return false;
        if (!CompareWithExist(connList,  slaves[i]. Master)) return false;
        if (!CompareWithExist(connList,  slaves[i]. Mail)) return false;
    }

     return true;
 }

bool ConfigChecker::CheckDBConnection()
{
    if (!_PROC->AddProcessor( g_proc_id,  "master.Manage", 1,   master. Manage.c_str())) return false;
    master.Proc_Manage = g_proc_id;
    g_proc_id ++;

    if (!_PROC->AddProcessor( g_proc_id,  "master.Role", 1,   master. Role.c_str())) return false;
    master.Proc_Role = g_proc_id;
    g_proc_id ++;

    if (!_PROC->AddProcessor( g_proc_id,  "master.Family", 1,   master. Family.c_str())) return false;
    master.Proc_Family = g_proc_id;
    g_proc_id ++;

    if (!_PROC->AddProcessor( g_proc_id,  "master.Master", 1,   master. Master.c_str())) return false;
    master.Proc_Master = g_proc_id;
    g_proc_id ++;

    if (!_PROC->AddProcessor( g_proc_id,  "master.Mail", 1,   master. Mail.c_str())) return false;
    master.Proc_Mail = g_proc_id;
    g_proc_id ++;

    for (size_t i=0; i < slaves.size(); i++)
    {
        char s[32];
        sprintf(s, "slaves[%d].", i);
        string n = s;

        if (!_PROC->AddProcessor( g_proc_id,  (n + "Manage").c_str(), 1,   slaves[i]. Manage.c_str())) return false;
        slaves[i].Proc_Manage = g_proc_id;
        g_proc_id ++;

        if (!_PROC->AddProcessor( g_proc_id,   (n + "Role").c_str(), 1,   slaves[i]. Role.c_str())) return false;
        slaves[i].Proc_Role = g_proc_id;
        g_proc_id ++;

        if (!_PROC->AddProcessor( g_proc_id,   (n + "Family").c_str(), 1,   slaves[i]. Family.c_str())) return false;
        slaves[i].Proc_Family = g_proc_id;
        g_proc_id ++;

        if (!_PROC->AddProcessor( g_proc_id,   (n + "Master").c_str(), 1,   slaves[i]. Master.c_str())) return false;
        slaves[i].Proc_Master = g_proc_id;
        g_proc_id ++;

        if (!_PROC->AddProcessor( g_proc_id,   (n + "Mail").c_str(), 1,   slaves[i].Mail.c_str())) return false;
        slaves[i].Proc_Mail= g_proc_id;
        g_proc_id ++;
    }

    return true;
}

void ConfigChecker::PrintConfig()
{

}


